package cn.gailvlun.gll.presentation.hotel.drawer

import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.gailvlun.gll.R
import cn.gailvlun.gll.bean.PlaceHolder
import cn.gailvlun.gll.databinding.FragmentHotelShareTextBinding
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.ListRes
import cn.gailvlun.gll.net.NewQueryParam
import cn.gailvlun.gll.net.hotel.InsomniaHotel
import cn.gailvlun.gll.net.hotel.InsomniaType
import cn.gailvlun.gll.presentation.base.BaseFragment
import cn.gailvlun.gll.presentation.hotel.widget.DividerDecoration
import cn.gailvlun.gll.util.DialogUtil
import cn.gailvlun.gll.util.IsEmpty
import cn.gailvlun.gll.util.RxUtil
import cn.gailvlun.gll.util.ToastUtil
import com.github.markzhai.recyclerview.BaseViewAdapter
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener


class HotelShareTextFragment : BaseFragment(), OnRefreshListener, OnLoadmoreListener {
    private lateinit var mBinding: FragmentHotelShareTextBinding

    private lateinit var mAdapter: HotelTextShareAdapter
    private lateinit var mLayoutManager: LinearLayoutManager

    private val newQueryParam = NewQueryParam()

    override fun initComponent(inflater: LayoutInflater, container: ViewGroup?): View {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_hotel_share_text, container, false)

        mAdapter = HotelTextShareAdapter(mContext)
        mAdapter.setPresenter(Presenter())
        mBinding.recyclerView.adapter = mAdapter

        mLayoutManager = LinearLayoutManager(mContext)
        mBinding.recyclerView.layoutManager = mLayoutManager
        mBinding.recyclerView.addItemDecoration(DividerDecoration(mContext,R.color.transparent))

        mBinding.srl.setOnRefreshListener(this)
        return mBinding.root
    }

    override fun createHandler() {

    }

    override fun loadData(savedInstanceState: Bundle?) {
        mBinding.srl.autoRefresh()
    }

    override fun onRefresh(refreshlayout: RefreshLayout?) {
        HttpMethods.getHotelService()
                .getMineInsomniaList(InsomniaType.T, null, null, null, null, null)
                .compose(RxUtil.applyScheduler())
                .subscribe(object : HttpSubscriber<ListRes<InsomniaHotel>>() {
                    override fun onSuccess(response: ListRes<InsomniaHotel>?) {
                        loadRecyclerView(response?.results, true, response?.isNext)
                    }

                    override fun onFailure(errMsg: String?, response: ListRes<InsomniaHotel>?, code: Int) {
                        DialogUtil.showError(mContext, errMsg)
                    }

                    override fun onFinish() {
                        super.onFinish()
                        mBinding.srl.finishRefresh()
                    }
                })
    }

    override fun onLoadmore(refreshlayout: RefreshLayout?) {
        newQueryParam.lastId = mAdapter.getLastId()
        newQueryParam.direction = NewQueryParam.next
        HttpMethods.getHotelService()
                .getMineInsomniaList(InsomniaType.T, null, null, newQueryParam.lastId,
                        newQueryParam.direction, null)
                .compose(RxUtil.applyScheduler())
                .subscribe(object : HttpSubscriber<ListRes<InsomniaHotel>>() {
                    override fun onSuccess(response: ListRes<InsomniaHotel>?) {
                        loadRecyclerView(response?.results, false, response?.isNext)
                    }

                    override fun onFailure(errMsg: String?, response: ListRes<InsomniaHotel>?, code: Int) {
                        DialogUtil.showError(mContext, errMsg)
                    }

                    override fun onFinish() {
                        super.onFinish()
                        mBinding.srl.finishLoadmore()
                    }
                })
    }

    fun loadRecyclerView(list: List<InsomniaHotel>?, isRefresh: Boolean, isMore: Boolean?) {
        if (isRefresh) {
            mAdapter.clear()
            if (IsEmpty.list(list)) {
                
            } else {
                mAdapter.addAll(list)
            }
        } else {
            mAdapter.addAll(list)
        }

        mBinding.srl.isEnableLoadmore = isMore!!
    }

    override fun isLazyLoad() = true

    inner class Presenter : BaseViewAdapter.Presenter {
        fun onDelete(item: InsomniaHotel) {
            val pd = DialogUtil.showIndeterminate(mContext)
            HttpMethods.getHotelService()
                    .deleteInsomnia(item.id)
                    .compose(RxUtil.applyScheduler())
                    .compose(bindToLifecycle())
                    .subscribe(object : HttpSubscriber<PlaceHolder>() {
                        override fun onSuccess(response: PlaceHolder?) {
                            ToastUtil.showShort(getString(R.string.hotel_drawer_audio_deletion))
                            mAdapter.deleteItem(item)
                        }

                        override fun onFailure(errMsg: String?, response: PlaceHolder?, code: Int) {
                            DialogUtil.showError(mContext, errMsg)
                        }

                        override fun onFinish() {
                            super.onFinish()
                            pd.dismiss()
                        }
                    })
        }
    }

}